<?php
declare (strict_types=1);

namespace app\common\model\user;

use cores\BaseModel;

/**
 *
 * Class Application
 * @package app\common\model\user
 */
class Application extends BaseModel
{
    // 定义表名
    protected $name = 'user_application';

    // 定义主键
    protected $pk = 'application_id';

    /**
     * 追加字段
     * @var array
     */
    protected $append = ['last_paper'];

    /**
     * 学员详情
     * @param $where
     * @param array $with
     * @return array|static|null
     */
    public static function detail($where, array $with = [])
    {
        return static::get($where, $with);
    }


    public function channel()
    {
        return $this->hasOne('app\common\model\store\User', 'store_user_id', 'channel_id');
    }

    public function user()
    {
        return $this->hasOne('app\common\model\User', 'user_id', 'user_id');
    }

    public function subject()
    {
        return $this->hasOne('app\common\model\subject\Job', 'subject_id', 'subject_id');
    }

    public function subjectLevel()
    {
        return $this->hasOne('app\common\model\subject\Level', 'subject_level_id', 'subject_level_id');
    }

    public function faceFile()
    {
        return $this->hasOne('app\\common\\model\\UploadFile', 'file_id', 'face_file_id');
    }

    public function comsignFile()
    {
        return $this->hasOne('app\\common\\model\\UploadFile', 'file_id', 'comsign_file_id');
    }

    public function examsignFile()
    {
        return $this->hasOne('app\\common\\model\\UploadFile', 'file_id', 'examsign_file_id');
    }

    public function getLastPaperAttr($value, $data)
    {
        return (new Paper())->where([
            'application_id' => $data['application_id'],
            'is_delete' => 0,
        ])->order('paper_id', 'desc')->find();
    }

}
